from __future__ import unicode_literals

from django.db import models
from django.contrib import admin
from django.utils.safestring import mark_safe
from django.utils import timezone
from sortedm2m.fields import SortedManyToManyField


class App(models.Model):
    name = models.CharField(max_length=50)
    version = models.IntegerField(default=1)
    force_update_version = models.IntegerField(default=1)
    level_order = SortedManyToManyField("Level", related_name='level_order', blank=True)
    
    def __unicode__(self):
        return self.name

    class Meta:
        unique_together = ["name",]


class Level(models.Model):
    name = models.CharField(max_length=100)
    image = models.CharField(max_length=400, default="", verbose_name="image", help_text='format: 1_5.jpg')
    ios = models.CharField(max_length=400, default="", help_text='format: 1_5')
    android = models.CharField(max_length=400, default="", help_text='format: 1_5a', blank=True)
    sound = models.ForeignKey("Sound", related_name='level_sound', on_delete=models.SET_NULL, null=True)

    ios_md5 = models.CharField(max_length=35, blank=True)
    android_md5 = models.CharField(max_length=35, blank=True)

    def display(self):
        return mark_safe(u'<img src="%s" width="120px" />' % self.image)
    display.short_description = u'graph'
    
    def __unicode__(self):
        return self.name

    class Meta:
        unique_together = ["name",]


class Sound(models.Model):
    name = models.CharField(max_length=100)
    sound = models.CharField(max_length=400, default="", help_text='format: 7')  # URL
    sound_android = models.CharField(max_length=400, default="", help_text='format: 7a')  # URL
    md5 = models.CharField(max_length=35, blank=True)
    md5_android = models.CharField(max_length=35, blank=True)

    def __unicode__(self):
        return self.name

    class Meta:
        unique_together = ["name",]